package com.linkage.jz.bankruptcy;

import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;

import com.igexin.sdk.PushManager;
import com.linkage.jz.bankruptcy.bean.LoginInfo;
import com.linkage.jz.bankruptcy.bean.ManagerUserInfo;
import com.linkage.jz.bankruptcy.bean.TokenInfo;
import com.linkage.jz.bankruptcy.net.ToastUtil;
import com.linkage.jz.bankruptcy.push.GTPushIntentService;
import com.linkage.jz.bankruptcy.push.GTPushService;
import com.linkage.jz.bankruptcy.utils.Utils;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.crashreport.CrashReport;
//import com.tencent.bugly.Bugly;
//import com.tencent.bugly.crashreport.CrashReport;

import java.lang.ref.SoftReference;

public class BankruptcyApp extends Application {

    public static BankruptcyApp mInstance;

    public static TokenInfo tokeninfo;
    public static LoginInfo loginInfo;
    public static String userToken;

    public static String online;


    public static String mUserid;
    public static String mUsername;
    public static String mphone;


    /*
     * 0.管理员
     * 1.
     * 2.
     * 10.债权人
     * 20.其他用户
     */
    public static final int PROPERMISSION_MANAGER = 0; // 管理员
    public static final int PROPERMISSION_ZQR = 10; // 债权人
    public static final int PROPERMISSION_OTHER = 20; // 其他用户
    public static final int PROPERMISSION_G = 30; // 其他用户
    public static int curProPermission;



    public static Activity activityRef;
    /*
     *  true 债权人
     *  flase 管理
     */
    public static Boolean isZQFlag;
    public static ManagerUserInfo selManger;
    /*
     * app创建方法
     */
    @Override
    public void onCreate() {
//        UMConfigure.init(this, "5cfdeb894ca357bc94000522", "5cfdeb894ca357bc94000522", UMConfigure.DEVICE_TYPE_PHONE, null);
//        UMConfigure.setLogEnabled(true);
        super.onCreate();
        mInstance = this;
        init();
        ToastUtil.init(this);
        PushManager.getInstance().initialize(getApplicationContext(), GTPushService.class);
        // com.getui.demo.DemoIntentService 为第三方自定义的推送服务事件接收类
        PushManager.getInstance().registerPushIntentService(getApplicationContext(), GTPushIntentService.class);

        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(getApplicationContext());
        CrashReport.initCrashReport(getApplicationContext(), "874e475bc6", true, strategy);


        CrashReport.postCatchedException(new Exception());
        Bugly.init(getApplicationContext(), "874e475bc6", false);
        CrashReport.setUserId("CrashReport");
        CrashReport.initCrashReport(getApplicationContext());
    }

    /*
     * 初始化参数
     */
    private void init() {
        Utils.init(this);
        loginInfo = null;
//        Hawk.init(this);
        ImageLoaderConfiguration config =
                new ImageLoaderConfiguration.Builder(getApplicationContext()).threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory().diskCacheFileNameGenerator(new Md5FileNameGenerator()).diskCacheSize(50 * 1024 * 1024) // 50 Mb
                .tasksProcessingOrder(QueueProcessingType.LIFO).writeDebugLogs() // Remove for release app
                .build();
        ImageLoader.getInstance().init(config);
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
//                MyActivityManager.getInstance().setCurrentActivity(activity);
                activityRef =  activity;
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });


    }

    private ImageLoader mAvatarImageLoader;

    public ImageLoader getAvatarImageLoader() {
        if (mAvatarImageLoader == null) {
            mAvatarImageLoader = ImageLoader.getInstance();

            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.avatar_defult)
                    // 设置图片在下载期间显示的图片
                    .showImageForEmptyUri(R.drawable.avatar_defult)
                    // 设置图片Uri为空或是错误的时候显示的图片
                    .showImageOnFail(R.drawable.avatar_defult)
                    // 设置图片加载/解码过程中错误时候显示的图片
                    // .resetViewBeforeLoading(true)
                    // .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                    .cacheOnDisk(true)
                    // 设置下载的图片是否缓存在SD卡中
                    // .considerExifParams(false)
                    // 是否考虑JPEG图像EXIF参数（旋转，翻转）
                    // .resetViewBeforeLoading(false)
                    // 设置图片在下载前是否重置，复位
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    // .displayer(new FadeInBitmapDisplayer(300))
                    .build();// 构建完成

            ImageLoaderConfiguration config = new ImageLoaderConfiguration
                    .Builder(this)
                    .threadPoolSize(2)
                    .threadPriority(Thread.NORM_PRIORITY - 2)
                    // .denyCacheImageMultipleSizesInMemory()
                    .imageDownloader(new BaseImageDownloader(this))
                    .defaultDisplayImageOptions(options)
                    .build();

            mAvatarImageLoader.init(config);
        }
        return mAvatarImageLoader;
    }


    public static BankruptcyApp getInstance() {
        return mInstance;
    }


}
